package modules.lobby
{
	import flash.utils.Dictionary;

	import communication.CertManager;

	import modules.DataManager;
	import modules.lobby.data.LobbyDataModel;
	import modules.lobby.data.NormalTableModel;
	import modules.lobby.data.TableDataListSortFilter;

	import protobuf.HSPKG_SELECT_TABLE_ACK;
	import protobuf.TABLE_BRIEF_INFO;

	/**
	 *
	 * @author warden_feng
	 * 2013-5-6
	 */
	public class LobbyDataManager extends DataManager
	{
		public function get certManager():CertManager
		{
			return CertManager.getInstance();
		}

		public function LobbyDataManager()
		{
			init();
		}

		private function init():void
		{
			dispatcher.addEventListener(LobbyEvent.GET_TABLE_LIST, onGetTableList);
		}

		/**
		 * 获取桌子列表数据
		 */
		private function onGetTableList(event:LobbyEvent):void
		{
			var tableDataList:HSPKG_SELECT_TABLE_ACK = event.data;

			//logger("获取桌子列表数据");

			var tableList:Array = [];
			var tableDic:Dictionary = new Dictionary();

			for each (var tableBriefInfo:TABLE_BRIEF_INFO in tableDataList.tables)
			{
				var table:NormalTableModel = new NormalTableModel();
				table.tableID = tableBriefInfo.tableId;
				var tableName:String = tableBriefInfo.tableName.readUTFBytes(tableBriefInfo.tableName.length);
				var colorSignIndex:int = tableName.indexOf("[#");
				if (colorSignIndex != -1)
				{
					table.tableName = tableName.substring(0, colorSignIndex);

					table.tableNameColor = parseInt(tableName.substring(colorSignIndex + 2, tableName.indexOf("]")), 16);
				}
				else
				{
					table.tableName = tableName;
				}
				table.rake = tableBriefInfo.rake; //抽成比例
				table.ip = tableBriefInfo.ip.readUTFBytes(tableBriefInfo.ip.length);
				table.port = tableBriefInfo.port;
				table.type = tableBriefInfo.type;
				table.minBlinds = Number(tableBriefInfo.minBlinds.toString().substr(0, 3)); //截取前三位
				table.maxBlinds = Number(tableBriefInfo.maxBlinds.toString().substr(0, 3)); //截取前三位
				table.minBuyAmount = tableBriefInfo.minBuyAmount;
				table.maxBuyAmount = tableBriefInfo.maxBuyAmount;
				table.responseTime = tableBriefInfo.responseTime;
				table.discardTime = tableBriefInfo.discardTime;
				table.tileCount = tableBriefInfo.tileCount;

				table.maxNum = tableBriefInfo.maxPlayerCount;
				table.seatedNum = tableBriefInfo.playerCount;

				table.hasPassword = false;

				table.vppRequiredLevel = GlobalData.getVPPLevel(tableBriefInfo.vppRequired);
				table.operator = certManager.convertByteArrayToString(tableBriefInfo.operator);
				table.currency = certManager.convertByteArrayToString(tableBriefInfo.currency);

				tableList.push(table);
				tableDic[table.tableID.toString()] = table;
			}

			LobbyDataModel.cashTableData = tableList;
			LobbyDataModel.cashTableDic = tableDic;

			TableDataListSortFilter.doSortFilter();
		}
	}
}
